﻿var totalNum = null;
/********************容器类 start***********************************************/
Rein.Control = Class.create({
    initialize: function (setting) {
        if (!this.setting) {
            this.setting = {};
        }
        this.init(setting);
    },
    init: function (setting) {
        this.setting = $.extend(true, {}, this.setting, setting);
    }
});

Rein.Holder = Class.create(Rein.Control, {
    initialize: function ($super, setting) {
        $super(setting);
    },
    init: function ($super, setting) {
        $super(setting);
        if (this.setting && this.setting.selector) {
            this.jobj = $(this.setting.selector);
            this.eobj = this.jobj[0];
        }
    },
});

Rein.KoHolder = Class.create(Rein.Holder, {
    initialize: function ($super, setting) {
        $super(setting);
    },
    
});

Rein.KoRepeater = Class.create(Rein.KoHolder, {
    initialize: function ($super, setting) {
        $super(setting);
    },
    
    loadData: function () {
        var vessel = this.setting.vessel;
        //第一次加载查询数据
        url2 = this.setting.queryUrl+"/"+ 0 + '/' + this.setting.size + '/';
        getData(url2, vessel, this.setting.conditions, this.setting.selector);

        //分页
        this.setPager(this.setting.pager, this.setting.queryUrl, vessel, this.setting.size, this.setting.conditions, this.setting.selector);
    },
    setPager: function (pageId, url, vessel, pageSize, conditions, selectorId) {
        $(pageId).pagination({
            totalData: totalNum,
            showData: 10,
            coping: true,
            homePage: '首页',
            endPage: '末页',
            prevContent: '上页',
            nextContent: '下页',
            callback: function (api) {
                vessel([]);
                var pageNum = api.getCurrent() - 1;
                var url2 = url + '/' + pageNum + '/' + pageSize + '/'
                getData(url2, vessel, conditions, selectorId);
            }
        });
    },
    //绑定字典项
    DictionariesList: function (url, refer) {
        $.ajax({
            async: true,
            type: "get",
            url: url,
            dataType: "json",
            success: function (result) {
                if (!!result) {
                    result.obj.unshift({ baseParentid: "-1", baseName: "全部" });
                    refer(result.obj);
                }
            },
            error: function (data) {
                alert(data)
            }
        });
        
    },
});

function getData(url, vessel, conditions, selectorId) {
    var p = selectorId.substr(1, selectorId.length);
    $.ajax({
        async: true,
        type: "get",
        url: url + JSON.stringify(conditions),//地址加参数
        //data: conditions,
        dataType: "json",
        beforeSend: function () {
            var loader = '<div class="LoaderAjax ajaxLoader' + p + '" ><img src ="img/loading.gif"></div>';
            $(selectorId).append(loader);
        },
        success: function (data) {
            $(".ajaxLoader" + p).remove();
            if (data) {
                totalNum = data.obj.totalElements;
                vessel(data.obj.content);
            }
        },
        error: function (data) {
            alert(data)
        }
    });
}
